/**
 * Copyright (c) 2014-2016 www.nryuncang.com. All Rights Reserved.
 */
package com.topisv.tms.repository.prioritySend;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.OrderBackTask;

/**
 * 
 *
 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
 * @version 1.0.0
 */
public interface OrderBackTaskDao extends PagingAndSortingRepository<OrderBackTask, Long>, JpaSpecificationExecutor<OrderBackTask> {

	@Modifying
	@Query(nativeQuery = true, value = "update order_back_task set node=?1 where status=?2 and node='-' order by id limit ?3")
	int lockTask(String node, String status, int maxRows);

	@Query("from OrderBackTask t where (t.status=?1 or t.status ='fail') and t.node=?2 and (t.retryTimes=?3 or t.retryTimes <3) order by t.id")
	Page<OrderBackTask> queryTask(String status, String node, int retryTimes, Pageable pageable);

	@Query("select count(t.id) from OrderBackTask t where t.id=?1 and t.status=?2")
	long countNotProcess(Long taskId, String status);

	@Modifying
	@Query("update OrderBackTask t set t.status = ?1, t.retryTimes = t.retryTimes + 1, updatedTime= ?3 where id = ?2")
	int updateStatus(String status, Long taskId, Date now);
}
